package com.ybear.ybcomponent.widget;

import android.graphics.Rect;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

public class SpaceItemDecoration extends RecyclerView.ItemDecoration {
    private final Rect mRect;
    public SpaceItemDecoration(int space, @RecyclerView.Orientation int orientation) {
        this(new Rect(
                orientation == RecyclerView.VERTICAL ? 0 : space,
                orientation == RecyclerView.VERTICAL ? space : 0,
                0,
                0
        ));
    }
    public SpaceItemDecoration(int space) {
        this( space, RecyclerView.VERTICAL );
    }

    public SpaceItemDecoration(Rect rect) {
        mRect = rect;
    }

    @Override
    public void getItemOffsets(@NonNull Rect outRect, @NonNull View view,
                               @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
//        super.getItemOffsets( outRect, view, parent, state );
        if( parent.getChildAdapterPosition( view ) == 0 ) return;
        outRect.set( mRect );
    }

    public int getSpace() { return mRect == null ? 0 : mRect.top; }

    public Rect getSpaces() {
        return mRect == null ? new Rect( 0, 0, 0, 0 ) : mRect;
    }
}
